package com.tianyang.yukuaifuzs.business.main.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseTitleActivity;
import com.tianyang.yukuaifuzs.business.auth.provider.AuthOpenProvider;
import com.tianyang.yukuaifuzs.business.main.bean.PosUserOutput;
import com.tianyang.yukuaifuzs.business.main.contract.MainConfig;
import com.tianyang.yukuaifuzs.business.main.contract.MerchantDetailContract;
import com.tianyang.yukuaifuzs.business.main.presenter.MerchantDetailPresenter;
import com.tianyang.yukuaifuzs.business.main.provider.MainOpenProvider;
import com.tianyang.yukuaifuzs.common.utils.CommonUtils;
import com.tianyang.yukuaifuzs.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：商户信息
 * Created by xyj on 2024/11/14
 */
public class MerchantDetailActivity extends BaseTitleActivity implements MerchantDetailContract.View {

    @BindView(R.id.bangdingcommercodeTv)
    TextView bangdingcommercodeTv;
    @BindView(R.id.merchantNameTv)
    TextView merchantNameTv;
    @BindView(R.id.shortNameTv)
    TextView shortNameTv;
    @BindView(R.id.posChannelNameTv)
    TextView posChannelNameTv;
    @BindView(R.id.mchIdTv)
    TextView mchIdTv;
    @BindView(R.id.mchTypeTv)
    TextView mchTypeTv;
    @BindView(R.id.bankActivityNameTv)
    TextView bankActivityNameTv;
    @BindView(R.id.authStatusTv)
    TextView authStatusTv;
    @BindView(R.id.signStatusTv)
    TextView signStatusTv;
    @BindView(R.id.settleCycleNameTv)
    TextView settleCycleNameTv;
    @BindView(R.id.wechatCodeTv)
    TextView wechatCodeTv;
    @BindView(R.id.wechatAuthorizeStatusTv)
    TextView wechatAuthorizeStatusTv;
    @BindView(R.id.aliCodeTv)
    TextView aliCodeTv;
    @BindView(R.id.aliAuthorizeStatusTv)
    TextView aliAuthorizeStatusTv;
    @BindView(R.id.createTimeTv)
    TextView createTimeTv;
    @BindView(R.id.devicesNumberTv)
    TextView devicesNumberTv;

    private MerchantDetailContract.Presenter mPresenter;

    private PosUserOutput mOutput = new PosUserOutput();

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_merchant_detail;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("商户信息");
        mPresenter = new MerchantDetailPresenter(this);

        PosUserOutput output = (PosUserOutput) getIntent().getSerializableExtra(MainConfig.INTENT_POS_USER_OUTPUT);
        if (output != null) {
            mOutput = output;
            bangdingcommercodeTv.setText(VerifyUtil.checkString(output.getBangdingcommercode()));
            merchantNameTv.setText(VerifyUtil.checkString(output.getMerchantName()));
            shortNameTv.setText(VerifyUtil.checkString(output.getShortName()));
            posChannelNameTv.setText("通道："+output.getPosChannelName());
            mchIdTv.setText("通道商户号："+output.getMchId());
            bankActivityNameTv.setText("项目名称："+output.getBankActivityName());
            settleCycleNameTv.setText("结算方式："+output.getSettleCycleName());
            createTimeTv.setText("入网日期："+VerifyUtil.checkString(output.getCreateDate()));
            wechatCodeTv.setText(VerifyUtil.checkString(output.getWechatCode()));
            if (TextUtils.equals("AUTHORIZED", output.getWechatAuthorizeStatus())) {
                wechatAuthorizeStatusTv.setText(" | 已授权");
                wechatAuthorizeStatusTv.setTextColor(ContextCompat.getColor(mContext, R.color.color_base));
            } else {
                wechatAuthorizeStatusTv.setText(" | 未授权");
                wechatAuthorizeStatusTv.setTextColor(ContextCompat.getColor(mContext, R.color.c_999999));
                wechatAuthorizeStatusTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        MainOpenProvider.getInstance().openMerchantInfoAuthActivity(mContext, mOutput, true);
                    }
                });
            }
            aliCodeTv.setText(VerifyUtil.checkString(output.getAliCode()));
            if (TextUtils.equals("AUTHORIZED", output.getAlipayAuthorizeStatus())) {
                aliAuthorizeStatusTv.setText(" | 已授权");
                aliAuthorizeStatusTv.setTextColor(ContextCompat.getColor(mContext, R.color.color_base));
            } else {
                aliAuthorizeStatusTv.setText(" | 未授权");
                aliAuthorizeStatusTv.setTextColor(ContextCompat.getColor(mContext, R.color.c_999999));
                aliAuthorizeStatusTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        MainOpenProvider.getInstance().openMerchantInfoAuthActivity(mContext, mOutput, false);
                    }
                });
            }
            switch (output.getMchType()) {
                case 0:
                    mchTypeTv.setText("商户类型：企业商户");
                    break;
                case 1:
                    mchTypeTv.setText("商户类型：小微商户");
                    break;
                case 2:
                    mchTypeTv.setText("商户类型：个体商户");
                    break;
            }
            if (output.getPosStatus() < 3) {
                authStatusTv.setText("待入网");
                authStatusTv.setTextColor(ContextCompat.getColor(mContext, R.color.c_999999));
            } else {
                authStatusTv.setText("已入网");
                authStatusTv.setTextColor(ContextCompat.getColor(mContext, R.color.color_base));
            }
            if (output.getSignStatus() > 0) {
                signStatusTv.setText("已签约");
                signStatusTv.setTextColor(ContextCompat.getColor(mContext, R.color.color_base));
            } else {
                authStatusTv.setText("未签约 ");
                signStatusTv.setTextColor(ContextCompat.getColor(mContext, R.color.c_999999));
            }
            if (!TextUtils.isEmpty(output.getDevicesNumber())) {
                devicesNumberTv.setText(output.getDevicesNumber());
            }
        }
    }

    @OnClick({R.id.copyPtRtv, R.id.copyMerchantRtv, R.id.copyShortRtv, R.id.copyMchRtv, R.id.posChannelLl, R.id.devicesNumberLl})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.copyPtRtv:
                CommonUtils.copyText(mContext, mOutput.getBangdingcommercode());
                break;
            case R.id.copyMerchantRtv:
                CommonUtils.copyText(mContext, mOutput.getMerchantName());
                break;
            case R.id.copyShortRtv:
                CommonUtils.copyText(mContext, mOutput.getShortName());
                break;
            case R.id.copyMchRtv:
                CommonUtils.copyText(mContext, mOutput.getMchId());
                break;
            case R.id.posChannelLl:
                AuthOpenProvider.getInstance().openAuthByMchType(mContext, mOutput.getMchType(),
                        mOutput.getPosStatus() + "", true, mOutput.getPhone());
                break;
            case R.id.devicesNumberLl:
                MainOpenProvider.getInstance().openDeviceListActivity(getContext(), mOutput.getUserId());
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MerchantDetailContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
     }
}
